/**
 * Project: jiangyao
 * <p>
 * File Created at 2020/8/17
 * <p>
 * Copyright 2015 www.showstart.com Croporation Limited.
 * All rights reserved.
 * <p>
 * This software is the confidential and proprietary information of
 * Taihe Music Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with www.showstart.com.
 */
package com.jiangyao.component;

import com.alibaba.fastjson.JSON;
import com.jiangyao.common.constants.CacheConstants;
import com.jiangyao.common.exception.ServerException;
import com.jiangyao.common.utils.DateUtil;
import com.jiangyao.component.sms.chuanglan.model.request.SmsSendRequest;
import com.jiangyao.component.sms.chuanglan.util.ChuangLanSmsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 短信发送组件
 *
 * @author: yangyongchaun
 * @create: 2020年08月17日 17:33
 */
@Slf4j
@Component
public class SMSComponent {

	/**
	 * 验证码短信模板
	 */
	private final static String LOGIN_CODE_SMS_TEMPLATE = "【匠要】验证码{s1}，您正在登录，若非本人操作，请勿泄露。";
	/**
	 * 通知短信模板-（可能很多通知模板）
	 */
	private final static String NOTICE_CODE_SMS_TEMPLATE = "【匠要】验证码{s1}，您正在登录，若非本人操作，请勿泄露。";

	/**
	 * 通知短信模板-（可能很多通知模板）
	 */
	private final static String NOTICE_SENDRESUME_SMS_TEMPLATE = "【匠要】用户{userName}向{group}公司投递了{jobName}岗位简历，请及时通知其查看处理";

	/**
	 * 验证码-接口URL
	 */
	private static final String CODE_API_URL = "http://smssh1.253.com/msg/send/json";
	/**
	 * 验证码-账号
	 */
	private static final String CODE_ACCOUNT = "YZM3524070";
	/**
	 * 验证码-密码
	 */
	private static final String CODE_PASSWORD = "EkcQl2j9Xr1792";

	/**
	 * 通知-接口URL
	 */
	private static final String NOTICE_API_URL = "http://smssh1.253.com/msg/send/json";
	/**
	 * 通知-账号
	 */
	private static final String NOTICE_ACCOUNT = "N6022244";
	/**
	 * 通知-密码
	 */
	private static final String NOTICE_PASSWORD = "32jrikB8x";


	@Autowired
	private RedisComponent redisComponent;

	/**
	 * 发送验证码短信
	 *
	 * @param phone 手机号
	 * @param code  验证码
	 */
	public void sendCode(String phone, String code) {
		checkSMSLimit(phone);
		// 短信内容
		String msg = LOGIN_CODE_SMS_TEMPLATE.replace("{s1}", code);
		//状态报告
		String report = "true";
		SmsSendRequest smsSingleRequest = new SmsSendRequest(CODE_ACCOUNT, CODE_PASSWORD, msg, phone, report);
		String requestJson = JSON.toJSONString(smsSingleRequest);
		log.debug("发送短信，requestJson：{}", requestJson);
		String response = ChuangLanSmsUtil.sendSmsByPost(CODE_API_URL, requestJson);
		log.info("发送短信，response result ：{}", response);
//		if(StringUtils.isNotBlank(response)){
//			SmsSendResponse smsSingleResponse = JSON.parseObject(response, SmsSendResponse.class);
//		}
	}


	/**
	 * 发送通知短信
	 *
	 * @param phone  手机号
	 * @param notice 通知
	 */
	public void sendNotice(String phone, String notice) {
		checkSMSLimit(phone);
		// 短信内容
//		String msg = notice;
		// 临时测试 TODO
		String msg = NOTICE_CODE_SMS_TEMPLATE.replace("{s1}", notice);
		//状态报告
		String report = "true";
		SmsSendRequest smsSingleRequest = new SmsSendRequest(NOTICE_ACCOUNT, NOTICE_PASSWORD, msg, phone, report);
		String requestJson = JSON.toJSONString(smsSingleRequest);
		log.debug("发送短信，requestJson：{}", requestJson);
		String response = ChuangLanSmsUtil.sendSmsByPost(NOTICE_API_URL, requestJson);
		log.debug("发送短信，response result ：{}", response);
//		if(StringUtils.isNotBlank(response)){
//			SmsSendResponse smsSingleResponse = JSON.parseObject(response, SmsSendResponse.class);
//		}
	}

	/**
	 * 发送简历通知短信
	 *
	 * @param phone  手机号
	 * @param group 公司名称
	 * @param userName 用户
	 * @param jobName 岗位名称
	 */
	public void sendResume(String phone, String group,String userName,String jobName) {
		checkSMSLimit(phone);
		// 短信内容
//		String msg = notice;
		// 临时测试 TODO
		String msg = NOTICE_SENDRESUME_SMS_TEMPLATE.replace("{group}", group).replace("{userName}",userName).replace("{jobName}",jobName);
		//状态报告
		String report = "true";
		SmsSendRequest smsSingleRequest = new SmsSendRequest(NOTICE_ACCOUNT, NOTICE_PASSWORD, msg, phone, report);
		String requestJson = JSON.toJSONString(smsSingleRequest);
		log.debug("发送短信，requestJson：{}", requestJson);
		String response = ChuangLanSmsUtil.sendSmsByPost(NOTICE_API_URL, requestJson);
		log.debug("发送短信，response result ：{}", response);

	}



	/**
	 * 检查手机号 今天发送次数是否超多限制
	 *
	 * @param phone 手机号
	 */
	private void checkSMSLimit(String phone) {
		long expire = (DateUtil.getTodayEndTime() - System.currentTimeMillis()) / 1000;
		long count = redisComponent.incr(CacheConstants.CACHE_KEY_TOKEN_SMS_LIMIT + phone, 1, expire);
		if (count > 20) {
			log.error("今天短信次数超过限制,手机号：{}，发送次数：{}", phone, count);
			throw new ServerException(100, "今天短信次数超过限制");
		}
	}

}
